package com.you.atlas.biz.member.service.impl;

import com.you.atlas.biz.member.constant.MemberRegisteFailture;
import com.you.atlas.biz.member.constant.MemberRegisteVO;
import com.you.atlas.biz.member.service.MemberService;
import com.you.common.result.ServiceResult;
import com.you.common.security.encoding.SecurityEncoder;
import com.you.member.dal.dao.MemberDAO;
import com.you.member.dal.dao.dataobject.MemberDO;

public class MemberServiceImpl implements MemberService {

    private MemberDAO       memberDAO;
    private SecurityEncoder securityEncoder;

    public void setMemberDAO(MemberDAO memberDAO) {
        this.memberDAO = memberDAO;
    }

    public void setSecurityEncoder(SecurityEncoder securityEncoder) {
        this.securityEncoder = securityEncoder;
    }

    @Override
    public ServiceResult<MemberRegisteFailture> registe(MemberRegisteVO memberRegisteVO) {
        String email = memberRegisteVO.getEmail();
        String name = memberRegisteVO.getName();
        String password = memberRegisteVO.getPassword();
        String location = memberRegisteVO.getLocation();

        MemberDO memberDO = memberDAO.findByEmail(email);
        if (memberDO != null) {
            return ServiceResult.newFailureResult(MemberRegisteFailture.EMAIL_REGISTED);
        }
        memberDO = new MemberDO();
        memberDO.setEmail(email);
        memberDO.setName(name);
        memberDO.setPasswordMD5(securityEncoder.encodeString(password));
        memberDO.setLocation(location);
        memberDO.setFaceUrl("/img/member/default-face.jpg");
        memberDO.setFaceIconUrl("/img/member/default-face-icon.jpg");
        memberDAO.create(memberDO);
        ServiceResult<MemberRegisteFailture> result = ServiceResult.newSuccessResult();
        return result.setModule(memberDO);
    }

    @Override
    public MemberDO registeMiniAccount(MemberRegisteVO memberRegisteVO) {

        String name = memberRegisteVO.getName();
        String location = memberRegisteVO.getLocation();
        MemberDO memberDO = new MemberDO();
        memberDO.setName(name);
        memberDO.setLocation(location);
        if (memberRegisteVO.getFaceIconUrl() != null) {
            memberDO.setFaceIconUrl(memberRegisteVO.getFaceIconUrl());
            memberDO.setFaceUrl(memberRegisteVO.getFaceThumbUrl());
        } else {
            memberDO.setFaceUrl("/img/member/default-face.jpg");
            memberDO.setFaceIconUrl("/img/member/default-face-icon.jpg");
        }
        memberDAO.create(memberDO);
        return memberDO;
    }

    @Override
    public boolean modifyMemberFace(Long memberId, String faceThumbUrl, String faceIconUrl) {
        MemberDO memberDO = new MemberDO();
        memberDO.setId(memberId);
        memberDO.setFaceUrl(faceThumbUrl);
        memberDO.setFaceIconUrl(faceIconUrl);
        memberDAO.update(memberDO);
        return false;
    }

    @Override
    public boolean modify(MemberDO memberDO) {
        return memberDAO.update(memberDO);
    }

    @Override
    public ServiceResult<MemberRegisteFailture> setPassword(Long id, MemberRegisteVO memberRegisteVO) {
        MemberDO memberDO = memberDAO.findByEmail(memberRegisteVO.getEmail());
        if (memberDO != null) {
            return ServiceResult.newFailureResult(MemberRegisteFailture.EMAIL_REGISTED);
        }
        memberDO = memberDAO.find(id);
        memberDO.setEmail(memberRegisteVO.getEmail());
        memberDO.setLocation(memberRegisteVO.getLocation());
        memberDO.setPasswordMD5(securityEncoder.encodeString(memberRegisteVO.getPassword()));
        memberDAO.update(memberDO);
        return ServiceResult.newSuccessResult();
    }

}
